package com.atom.app.mvi.ui.compose

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.*
import androidx.compose.ui.graphics.*
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import java.util.*

class JetLaggedActivity : ComponentActivity() {

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { ContentView() }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    @Preview
    @Composable
    fun PreviewMessageCard() {
        ContentView()
    }

    @RequiresApi(Build.VERSION_CODES.O)
    @Composable
    fun ContentView() {
        Surface(
            modifier = Modifier.fillMaxSize(), color = Color.White
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
            ) {
                Column(modifier = Modifier.background(color = Color.Red)) {
                    //JetLaggedHeader
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(150.dp)
                    ) {
                        Row(modifier = Modifier.windowInsetsPadding(insets = WindowInsets.systemBars)) {
                            IconButton(onClick = { /*TODO*/ }) {
                                Icon(
                                    Icons.Default.ArrowBack, contentDescription = "null"
                                )
                            }
                            Text(
                                text = "JetLagged",
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(8.dp),
                                style = TextStyle(
                                    fontSize = 22.sp,
                                    fontWeight = FontWeight(700),
                                    letterSpacing = 0.5.sp,
                                    fontFamily = fontFamily
                                ),
                                textAlign = TextAlign.Start
                            )
                        }
                    }

                    Spacer(modifier = Modifier.height(32.dp))
                    //JetLaggedSleepSummary
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(start = 16.dp, end = 16.dp),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Column {
                            Text(
                                text = "AVG. TIME IN BED", style = TextStyle(
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight(600),
                                    letterSpacing = 0.5.sp,
                                    fontFamily = fontFamily
                                )
                            )
                            Text(
                                text = "8h2min", style = TextStyle(
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight(600),
                                    letterSpacing = 0.5.sp,
                                    fontFamily = fontFamily
                                )
                            )
                        }
                    }

                }

                Spacer(modifier = Modifier.height(16.dp))

                var selectedTab by remember { mutableStateOf(SleepTab.Week) }

                ScrollableTabRow(modifier = Modifier,
                    edgePadding = 12.dp,
                    selectedTabIndex = selectedTab.ordinal,
                    backgroundColor = Color.Yellow,
                    indicator = { tabPositions: List<TabPosition> ->
                        Box(
                            Modifier
                                .tabIndicatorOffset(tabPositions[selectedTab.ordinal])
                                .fillMaxSize()
                                .padding(horizontal = 2.dp)
                                .border(BorderStroke(2.dp, Color.Yellow), RoundedCornerShape(10.dp))
                        )
                    },
                    divider = { }) {
                    SleepTab.values().forEachIndexed { index, sleepTab ->
                        val selected = index == selectedTab.ordinal
                        Tab(modifier = Modifier
                            .padding(horizontal = 2.dp)
                            .clip(RoundedCornerShape(16.dp)),
                            selected = selected,
                            unselectedContentColor = Color.Black,
                            selectedContentColor = Color.Black,
                            onClick = {
                                selectedTab = SleepTab.values()[index]
                            }) {
                            Text(
                                modifier = Modifier.padding(vertical = 6.dp, horizontal = 4.dp),
                                text = stringResource(id = sleepTab.title),
                                style = TextStyle(
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight(600),
                                    letterSpacing = 0.5.sp,
                                    fontFamily = fontFamily
                                )
                            )
                        }
                    }
                }

                Spacer(modifier = Modifier.height(16.dp))

                //-----------------------------------------------------

                val sleepState by remember {
                    mutableStateOf(sleepData)
                }

                val scrollState = rememberScrollState()

                val hours = (sleepState.earliestStartHour..23) + (0..sleepState.latestEndHour)

                val dayLabels = @Composable {
                    repeat(sleepState.sleepDayData.size) { index ->
                        val data = sleepState.sleepDayData[index]
                        Text(
                            data.startDate.dayOfWeek.getDisplayName(
                                java.time.format.TextStyle.SHORT, Locale.getDefault()
                            ),
                            Modifier
                                .height(24.dp)
                                .padding(start = 8.dp, end = 24.dp),
                            style = SmallHeadingStyle,
                            textAlign = TextAlign.Center
                        )
                    }
                }

            }
        }
    }

    @Preview
    @RequiresApi(Build.VERSION_CODES.O)
    @Composable
    fun ContentView2() {
        Surface(
            modifier = Modifier.fillMaxSize(), color = Color.White
        ) {
            Image(
                painter = rememberAsyncImagePainter("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Fbcf80762-fe01-4be6-a3ed-9a31d8b35c12%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1677826269&t=c9a4245872bdd7e50ba28b8d4b6c225e"),
                contentDescription = "",
                modifier = Modifier.size(128.dp) ,
            )
        }
    }
}

